package org.jeecg.modules.demo.checkemployee.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import liquibase.pro.packaged.L;
import org.jeecg.modules.demo.applyapartemployee.entity.ZnzfApplyApartEmployee;
import org.jeecg.modules.demo.applyapartemployee.mapper.ZnzfApplyApartEmployeeMapper;
import org.jeecg.modules.demo.base.entity.ZnzfEmployeeScoreItem;
import org.jeecg.modules.demo.base.entity.ZnzfScoreStandard;
import org.jeecg.modules.demo.base.entity.ZnzfScoreStandardItem;
import org.jeecg.modules.demo.base.mapper.ZnzfEmployeeScoreItemMapper;
import org.jeecg.modules.demo.base.mapper.ZnzfScoreStandardItemMapper;
import org.jeecg.modules.demo.base.mapper.ZnzfScoreStandardMapper;
import org.jeecg.modules.demo.checkemployee.entity.ZnzfCheckEmployee;
import org.jeecg.modules.demo.checkemployee.mapper.ZnzfCheckEmployeeMapper;
import org.jeecg.modules.demo.checkemployee.service.IZnzfCheckEmployeeService;
import org.jeecg.modules.demo.roomapartdistribute.entity.ZnzfCheckApart;
import org.jeecg.modules.demo.roomapartdistribute.mapper.ZnzfCheckApartMapper;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 核定单位报名人员
 * @Author: jeecg-boot
 * @Date:   2022-03-21
 * @Version: V1.0
 */
@Service
public class ZnzfCheckEmployeeServiceImpl extends ServiceImpl<ZnzfCheckEmployeeMapper, ZnzfCheckEmployee> implements IZnzfCheckEmployeeService {

    @Resource
    ZnzfCheckEmployeeMapper znzfCheckEmployeeMapper;

    @Resource
    ZnzfCheckApartMapper znzfCheckApartMapper;

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    ZnzfApplyApartEmployeeMapper znzfApplyApartEmployeeMapper;

    @Resource
    ZnzfEmployeeScoreItemMapper znzfEmployeeScoreItemMapper;

    @Resource
    ZnzfScoreStandardItemMapper znzfScoreStandardItemMapper;

    @Resource
    ZnzfScoreStandardMapper znzfScoreStandardMapper;

    /**
     * 根据房源单位分配表的id查询某一核定单位下的所有报名人员
     * @param id
     * @return
     */
    @Override
    public List<ZnzfCheckEmployee> findEmployListByApartId(String id) {
        LambdaQueryWrapper<ZnzfCheckEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfCheckEmployee::getApartId, id);
        List<ZnzfCheckEmployee> employeeList = znzfCheckEmployeeMapper.selectList(queryWrapper);
        return employeeList;
    }

    @Override
    public List<ZnzfCheckEmployee> findCheckedEmployListByCheckApartId(String id) {
        LambdaQueryWrapper<ZnzfCheckEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfCheckEmployee::getCheckApartId, id);
        List<ZnzfCheckEmployee> employeeList = znzfCheckEmployeeMapper.selectList(queryWrapper);
        return employeeList;
    }

    @Override
    public List<ZnzfCheckEmployee> findCheckedEmployListByCheckApartIdAndNum(String id, int num) {
        LambdaQueryWrapper<ZnzfCheckEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfCheckEmployee::getCheckApartId, id).eq(ZnzfCheckEmployee::getIsShortlisted, 1);
        queryWrapper.orderByDesc(ZnzfCheckEmployee::getScore).last("limit " + num);
        List<ZnzfCheckEmployee> employeeList = znzfCheckEmployeeMapper.selectList(queryWrapper);
        return employeeList;
    }

    /**
     * 根据房源单位分配表的id查询某一核定单位下的所有已中签的报名人员
     * @param id
     * @return
     */
    @Override
    public List<ZnzfCheckEmployee> findCheckedEmployListByApartId(String id) {
        LambdaQueryWrapper<ZnzfCheckEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfCheckEmployee::getApartId, id).eq(ZnzfCheckEmployee::getIsShortlisted, 1);
        List<ZnzfCheckEmployee> employeeList = znzfCheckEmployeeMapper.selectList(queryWrapper);
        return employeeList;
    }

    /**
     * 根据核定单位报名人员表的id查询该人员的得分详情
     * @param id
     * @return
     */
    @Override
    public List<ZnzfScoreStandardItem> queryScoreDetailsById(String id) {
        ZnzfCheckEmployee znzfCheckEmployee = znzfCheckEmployeeMapper.selectById(id);
        //根据userId查询职员计分标准项表
        LambdaQueryWrapper<ZnzfEmployeeScoreItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployeeScoreItem::getUserId, znzfCheckEmployee.getUserId());
        List<ZnzfEmployeeScoreItem> list = znzfEmployeeScoreItemMapper.selectList(queryWrapper);
        List<ZnzfScoreStandardItem> arrayList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ZnzfEmployeeScoreItem znzfEmployeeScoreItem = list.get(i);
            ZnzfScoreStandardItem znzfScoreStandardItem  = znzfScoreStandardItemMapper.selectById(znzfEmployeeScoreItem.getScoreItemId());
            ZnzfScoreStandard znzfScoreStandard = znzfScoreStandardMapper.selectById(znzfScoreStandardItem.getScoreStandardId());
            //从计分标准表中获取标题信息，并设置计分标准项的标题
            znzfScoreStandardItem.setTitle(znzfScoreStandard.getTitle());
            arrayList.add(znzfScoreStandardItem);
        }

        return arrayList;
    }

    /**
     * 根据房源单位分配表的id和姓名模糊查询某一核定单位下的所有已入围的报名人员
     * @param apartId
     * @param name
     * @return
     */
    @Override
    public List<ZnzfCheckEmployee> findCheckedEmployListByApartIdAndName(String apartId, String name) {

        LambdaQueryWrapper<ZnzfCheckEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfCheckEmployee::getApartId, apartId).eq(ZnzfCheckEmployee::getIsShortlisted, 1)
                .like(name!=null,ZnzfCheckEmployee::getCreateBy, name);
        return znzfCheckEmployeeMapper.selectList(queryWrapper);
    }

    /**
     *
     * @param userId
     * @return
     */
    @Override
    public String findTelByUserId(String userId) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getId, userId);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        if (sysUser == null) return null;
        return sysUser.getPhone();
    }


    /**
     * 根据用户id查找对应的部门
     * @param uid
     * @return
     */
    @Override
    public ZnzfCheckApart findApartByUserId(String uid) {
        LambdaQueryWrapper<ZnzfCheckEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfCheckEmployee::getUserId,uid);
        ZnzfCheckEmployee employee = znzfCheckEmployeeMapper.selectOne(queryWrapper);

        if (employee == null) {
            return null;
        } else {
            LambdaQueryWrapper<ZnzfCheckApart> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ZnzfCheckApart::getApartId, employee.getApartId());
            ZnzfCheckApart znzfCheckApart = znzfCheckApartMapper.selectOne(queryWrapper1);
            return znzfCheckApart;
        }
    }


    /**
    * @Description: 根据房源单位分配表的id查询
    *
    * @date: 2022/3/23 15:23
    * @param radId: 房源单位分配表的id
    * @Return: com.alibaba.fastjson.JSONArray
    */
    @Override
    public JSONArray getByRadId(String radId) {
        LambdaQueryWrapper<ZnzfCheckApart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfCheckApart::getRoomApartDistributeId, radId);
        List<ZnzfCheckApart> znzfCheckApartList = znzfCheckApartMapper.selectList(queryWrapper);

        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < znzfCheckApartList.size(); i++) {
            String id = znzfCheckApartList.get(i).getId();
            LambdaQueryWrapper<ZnzfCheckEmployee> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ZnzfCheckEmployee::getCheckApartId, id);
            List<ZnzfCheckEmployee> znzfCheckEmployeeList = znzfCheckEmployeeMapper.selectList(queryWrapper1);
            JSONObject object = new JSONObject();
            object.put("sys_org_code", znzfCheckEmployeeList.get(i).getSysOrgCode());
            //根据user_id去sys_user表中查询真实姓名
            object.put("real_name", sysUserMapper.selectById(znzfCheckEmployeeList.get(i).getUserId()).getRealname());
            object.put("apply_time", znzfCheckEmployeeList.get(i).getApplyTime());
            object.put("score", znzfCheckEmployeeList.get(i).getScore());
            object.put("rank", znzfCheckEmployeeList.get(i).getRank());
            jsonArray.add(object);
        }
//        List list = JSONArray.parseArray(jsonArray.toJSONString());
        return jsonArray;
    }

    @Override
    public List<ZnzfCheckEmployee> getByCheckApartId(String caId) {
        ZnzfCheckApart znzfCheckApart = znzfCheckApartMapper.selectById(caId);
        List<JSONObject> jsonObjectList = new ArrayList<>();
        String id = znzfCheckApart.getId();
        LambdaQueryWrapper<ZnzfCheckEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfCheckEmployee::getCheckApartId, id);
        List<ZnzfCheckEmployee> znzfCheckEmployeeList = znzfCheckEmployeeMapper.selectList(queryWrapper);
        for(ZnzfCheckEmployee znzfCheckEmployee : znzfCheckEmployeeList){
            SysUser sysUser = sysUserMapper.selectById(znzfCheckEmployee.getUserId());
            znzfCheckEmployee.setRealName(sysUser.getRealname());
            znzfCheckEmployee.setTel(sysUser.getPhone());
        }
//        List list = JSONArray.parseArray(jsonArray.toJSONString());
        return znzfCheckEmployeeList;
    }


}
